clear all;

cd ..; cd ..; cd ..;
load('code_main_model\6_implied_w_r\computation_results\data.mat','Z','d','credit','Y');
cd 'code_figure_table\OA\Figure OA12';

ZZ=unique(Z);

% mkt_index1=find(Z==ZZ(3) & d(1,:)==median(d(1,:))); mkt_index1=mkt_index1(1);
mkt_index1=find(Z==ZZ(3) & abs(d(1,:)-median(d(1,:)))<1); mkt_index1=mkt_index1(1);
mkt_index2=find(Z==ZZ(3) & d(1,:)==0); mkt_index2=mkt_index2(1);

pop_min=100;
pop_max=200;

x=[pop_min, pop_max];
y_credit_ini=[pop_min, pop_max] * credit(1,mkt_index1);
y_credit_new=[pop_min, pop_max] * credit(1,mkt_index2);
y_credit_gap=y_credit_new-y_credit_ini;

y_output_ini=[pop_min, pop_max] * Y(1,mkt_index1);
y_output_new=[pop_min, pop_max] * Y(1,mkt_index2);
y_output_gap=y_output_new-y_output_ini;

figure;
plot(x,y_credit_ini);
hold on
plot(x,y_credit_new);
hold on
plot(x,y_credit_gap);

figure;
plot(x,y_output_ini);
hold on
plot(x,y_output_new);
hold on
plot(x,y_output_gap);

save('AD.mat','x','y_credit_ini','y_credit_new','y_credit_gap',...
    'y_output_ini','y_output_new','y_output_gap');